﻿using Manon.Admin.ApplicationCore.Common;
using Manon.Admin.ApplicationCore.Infrastructure;
using Manon.Admin.Web.ApplicationCore.Dtos.Other;
using Manon.Admin.Web.ApplicationCore.Entities.Other;
using Manon.Admin.Web.ApplicationCore.Interfaces;
using Manon.Autofac;
using Manon.Core.Commons;
using Manon.Repository;
using Manon.Repository.Collections;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Manon.Admin.Web.ApplicationCore.Services
{
    public class OtherService : IOtherService, ISingletonDependency
    {
        /// <summary>
        /// 获取友情链接
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<GetFriendlylinkOutput> GetFriendlylink(int Id)
        {
            if (Id <= 0) return new GetFriendlylinkOutput() { Enabled = true };

            var logger = Resolver.Resolve<ILogger<OtherService>>();
            try
            {


                var unitOfWork = Resolver.Resolve<IUnitOfWork<AdminDbContext>>();

                var result = await unitOfWork.GetRepository<FriendlyLink>().FindAsync(Id);
                return result.ConvertTo<FriendlyLink, GetFriendlylinkOutput>(); 
            }
            catch (Exception ex)
            {
                logger.LogError("获取资讯异常", ex);
                throw;
            }
        }

        /// <summary>
        /// 获取友情链接列表
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<IPagedList<GetFriendlylinkOutput>> GetFriendlylinkPagedList(GetFriendlylinkPagedListInput Input)
        {
            var logger = Resolver.Resolve<ILogger<OtherService>>();
            try
            {
                var unitOfWork = Resolver.Resolve<IUnitOfWork<AdminDbContext>>();
                var categorys = await unitOfWork.GetRepository<FriendlyLink>().GetAll().ToListAsync();


                Expression<Func<FriendlyLink, bool>> predicate = null;
                if (!string.IsNullOrEmpty(Input.Keyword))
                {
                    predicate = e => e.Name.Contains(Input.Keyword) || e.Remark.Contains(Input.Keyword);
                }
                if (Input.Enabled.HasValue)
                {
                    predicate = e => e.Enabled == Input.Enabled.Value;
                }
                var result = await unitOfWork.GetRepository<FriendlyLink>().GetPagedListAsync<GetFriendlylinkOutput>(predicate, Input);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("获取资讯列表异常", ex);
                throw;
            }
        }

        /// <summary>
        /// 保存友情链接 
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<Result> SaveFriendlylink(SaveFriendlylinkInput Input)
        {
            var logger = Resolver.Resolve<ILogger<RoleService>>();
            try
            {
                var identityUser = Resolver.Resolve<IIdentityUser>();
                var unitOfWork = Resolver.Resolve<IUnitOfWork<AdminDbContext>>();
                var repos = unitOfWork.GetRepository<FriendlyLink>();
                var result = await repos.FindAsync(Input.Id);
                if (result == null)
                {
                    result = new FriendlyLink();
                    result.ImageUrl = Input.ImageUrl;
                    result.Name = Input.Name;
                    result.OrderIndex = Input.OrderIndex;
                    result.Remark = Input.Remark;
                    result.Url = Input.Url;
                    result.Enabled = Input.Enabled;
                    result.CreateTime = DateTime.Now;
                    result.UpdateTime = DateTime.Now;
                    result.OrderIndex = Input.OrderIndex;
                    result.CreateBy = identityUser.UserId;
                    result.UpdateBy = identityUser.UserId;
                    await repos.InsertAsync(result);
                }
                else
                {
                    result.ImageUrl = Input.ImageUrl;
                    result.Name = Input.Name;
                    result.OrderIndex = Input.OrderIndex;
                    result.Remark = Input.Remark;
                    result.Url = Input.Url;
                    result.Enabled = Input.Enabled;
                    result.UpdateTime = DateTime.Now;
                    result.OrderIndex = Input.OrderIndex;
                    result.UpdateBy = identityUser.UserId;
                    repos.Update(result);
                }
                await unitOfWork.SaveChangesAsync();
                return Result.Successed("保存成功!");
            }
            catch (Exception ex)
            {
                logger.LogError("保存友情链接异常", ex);
                throw;
            }
        }


        /// <summary>
        /// 设置友情链接是否可用
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<Result> SetFriendlylinkEnabled(int Id, bool Enabled)
        {
            var logger = Resolver.Resolve<ILogger<RoleService>>();
            try
            {

                var unitOfWork = Resolver.Resolve<IUnitOfWork<AdminDbContext>>();

                var news = await unitOfWork.GetRepository<FriendlyLink>().FindAsync(Id);
                if (news == null) return null;
                news.Enabled = Enabled;
                await unitOfWork.SaveChangesAsync();
                return Result.Successed("设置成功!");

            }
            catch (Exception ex)
            {
                logger.LogError("设置友情链接是否可用异常", ex);
                throw;
            }
        }
    }
}
